import LeftCtrl from "./LeftCtrl";
import RightCtrl from "./RightCtrl";
import Player from "./Player";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MainScene extends cc.Component {

    @property(LeftCtrl)
    leftCtrl: LeftCtrl = null;
    @property(RightCtrl)
    rightCtrl: RightCtrl = null;
    @property(Player)
    player : Player = null;
    @property(cc.Node)
    ground : cc.Node = null;
    /** 移动速度 */
    private static readonly SPEED_MOVE = 200;
    /** 转向速度 */
    private static readonly SPEED_ROTATE = 1;
    start () {
        cc.director.getCollisionManager().enabled = true;
    };

    update (dt) {
        let rotateDt = Math.pow((this.rightCtrl.curPos - this.rightCtrl.lastPos)* 0.5, 2)*0.1;
        if (this.rightCtrl.curPos > this.rightCtrl.lastPos) {
            this.player.node.rotation += rotateDt;
        } else {
            this.player.node.rotation -= rotateDt;
        }
        this.rightCtrl.lastPos = this.rightCtrl.curPos;

        this.ground.rotation = - this.player.node.rotation;
        var a = this.leftCtrl.direction.rotate(-this.player.node.rotation/180*Math.PI);
        // console.log(this.leftCtrl.direction, a, this.player.rotation);
        a.mulSelf(dt * MainScene.SPEED_MOVE);
        // this.player.node.getComponent(cc.RigidBody).applyForceToCenter(a, true);
        this.player.node.position = this.player.node.position.add(a);
        this.player.setMoving(this.leftCtrl.direction.x !== 0 || this.leftCtrl.direction.y !== 0);
    };

}
